package client.ui.controllers;

/*
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 *
 * LoginScreen.java
 *
 * Steven Louie Apr. 8, 2012
 */
import client.ActinoClient;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.controls.Label;
import de.lessvoid.nifty.controls.TextField;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;

public class LoginScreen implements ScreenController {

    private Nifty nifty;

    @Override
    public void bind(Nifty nifty, Screen screen) {
        this.nifty = nifty;
    }

    @Override
    public void onStartScreen() {
    }

    @Override
    public void onEndScreen() {
    }

    public void login() {
        Label info = nifty.getCurrentScreen().findNiftyControl("info",
                Label.class);
        info.setText("");
        
        String user = nifty.getCurrentScreen().findNiftyControl("username",
                TextField.class).getText();
        String pass = nifty.getCurrentScreen().findNiftyControl("password",
                TextField.class).getText();
        if (user == null || user.equals("") || pass == null || pass.equals("")) {
            info.setText("Invalid username/password");
            return;
        }

        if (ActinoClient.getInstance().getKryoClient().isConnected()) {
            ActinoClient.getInstance().getTcp().sendLogin(user, pass);
        } else {
            info.setText("Could not connect to server");
        }
    }

    public void gotoRegister() {
        clearData();
        nifty.gotoScreen("register");
    }

    public void clearData() {
        nifty.getCurrentScreen().findNiftyControl("username",
                TextField.class).setText("");
        nifty.getCurrentScreen().findNiftyControl("password",
                TextField.class).setText("");
        nifty.getCurrentScreen().findNiftyControl("info",
                Label.class).setText("");
    }
}
